package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.MallMaterial;
import com.tencreat.mapper.mall.MallMaterialMapper;
import com.tencreat.service.mall.IMallMaterialService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 朋友圈素材Service业务层处理
 *
 * @author tencreat
 * @date 2022-01-10
 */
@Service
public class MallMaterialServiceImpl extends ServiceImpl<MallMaterialMapper, MallMaterial> implements IMallMaterialService {


    /**
     * 查询朋友圈素材
     *
     * @param id 朋友圈素材ID
     * @return 朋友圈素材
     */
    @Override
    public MallMaterial selectMallMaterialById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询朋友圈素材列表
     *
     * @param mallMaterial 朋友圈素材
     * @return 朋友圈素材
     */
    @Override
    public List<MallMaterial> selectMallMaterialList(MallMaterial mallMaterial) {
        return baseMapper.selectList(Wrappers.query(mallMaterial));
    }

    /**
     * 新增朋友圈素材
     *
     * @param mallMaterial 朋友圈素材
     * @return 结果
     */
    @Override
    public int insertMallMaterial(MallMaterial mallMaterial) {
        mallMaterial.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(mallMaterial);
    }

    /**
     * 修改朋友圈素材
     *
     * @param mallMaterial 朋友圈素材
     * @return 结果
     */
    @Override
    public int updateMallMaterial(MallMaterial mallMaterial) {
        mallMaterial.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallMaterial);
    }

    /**
     * 批量删除朋友圈素材
     *
     * @param ids 需要删除的朋友圈素材ID
     * @return 结果
     */
    @Override
    public int deleteMallMaterialByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除朋友圈素材信息
     *
     * @param id 朋友圈素材ID
     * @return 结果
     */
    @Override
    public int deleteMallMaterialById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    /****
     * 上升
     * @param mallMaterial
     * @return
     */
    @Override
    public boolean reSortOrderReduce(MallMaterial mallMaterial) {
        MallMaterial one = lambdaQuery().eq(MallMaterial::getId, mallMaterial.getId()).one();
        one.setSort(one.getSort() -1);
        updateById(one);

        return true;
    }

    /****
     * 下降
     * @param mallMaterial
     * @return
     */
    @Override
    public boolean reSortOrderPlus(MallMaterial mallMaterial) {
        MallMaterial one = lambdaQuery().eq(MallMaterial::getId, mallMaterial.getId()).one();
        one.setSort(one.getSort() +1);
        updateById(one);

        return true;
    }
}
